package com.wtwd.device.controller;


import com.wtwd.device.constant.RespCode;
import com.wtwd.device.model.vo.ResultVO;
import com.wtwd.device.model.vo.WeatherVO;
import com.wtwd.device.service.WeatherService;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zdl
 * @since 2021-08-10
 */
@Api(tags = "天气")
@RestController
@RequestMapping("/weather")
public class WeatherController {

    private static final Logger log = LoggerFactory.getLogger(WeatherController.class);

    @Resource
    private WeatherService weatherService;

    @ApiImplicitParams({
            @ApiImplicitParam(name = "lon", value = "经度", required = true, dataType = "String"),
            @ApiImplicitParam(name = "lat", value = "纬度", required = true, dataType = "String"),
            @ApiImplicitParam(name = "lang", value = "语言,中文-cn,其他-en", required = true, dataType = "String"),
            @ApiImplicitParam(name = "city", value = "城市,精确到区", dataType = "String"),
            @ApiImplicitParam(name = "province", value = "省", dataType = "String"),
            @ApiImplicitParam(name = "country", value = "国家", dataType = "String"),
            @ApiImplicitParam(name = "cid", value = "城市编码", dataType = "String"),
    })
    @ApiResponses({
            @ApiResponse(code = 2001, message = "该位置天气获取失败")
    })
    @ApiOperation(value = "获取天气情况")
    @GetMapping("/info")
    public ResultVO<WeatherVO> getWeather(@RequestParam(value = "city", required = false) String city,
                                          String lon, String lat, String lang, @RequestParam(value = "province", required = false) String province
            , String cid, @RequestParam(value = "country", required = false) String country, String appCid) {
        if (StringUtils.isAnyBlank(province, city) && StringUtils.isBlank(cid)) {
            return ResultVO.error(RespCode.PARAMS_ERROR);
        }
        WeatherVO weatherVO = weatherService.getGFWeather(city, cid, lon, lat, lang, province, country,appCid);
        if (weatherVO == null) {
            return ResultVO.error(RespCode.WEATHER_NOT_GET);
        }
        return ResultVO.success(weatherVO);
    }
}

